﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DALScolarite;
namespace Scolarite 
{
    public partial class EUECU : Form
    {
        public modeload loadingmod;
        public string idunite = "";

        public EUECU()
        {
            InitializeComponent();
        }

        private void unite_obligatoireCheckBox_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void idEltDiplomeLabel_Click(object sender, EventArgs e)
        {

        }

        private void idEltDiplomeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void EUECU_Load(object sender, EventArgs e)
        {

            scolarite_eltdiplome diplome = new scolarite_eltdiplome();
            scolariteeltdiplomeBindingSource.DataSource = diplome.diplome();

            scolaritetypeeltetudeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_typeeltetude.ToList();
        
         

            scolarite_domaine domaine = new scolarite_domaine();
           

            if (idunite != string.Empty)
            {
                scolaritedomaineBindingSource.DataSource = domaine.domaineue(idunite);
            }
            else
            {
                scolaritedomaineBindingSource.DataSource = domaine.domaineue();
            }

            scolaritesemestresBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_semestres.ToList();
            scolaritetypeuniteBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_typeunite.ToList();
            scolaritemodelregimecalculeBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_modelregimecalcule.ToList();
            if (this.loadingmod == modeload.AJOUT)
            {
                //scolarite_ensunitesBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_eltdiplome.ToList();
                scolarite_ensunitesBindingSource.AddNew();
            }
            else if (this.loadingmod == modeload.INFO && this.idunite != string.Empty)
            {
                scolarite_ensunitesBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_ensunites.Where(et => et.idunite == idunite).ToList();

            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
             try
            {
                scolarite_ensunites addue = new scolarite_ensunites 
                    {
                            idunite= iduniteTextBox.Text,
                            idTypeUnite = idTypeUniteComboBox.SelectedValue.ToString(),
                            idEltDiplome = idEltDiplomeComboBox.SelectedValue.ToString(),
                            idsemestres = idsemestresComboBox.SelectedValue.ToString(),
                            idModelcalcule = idModelcalculeComboBox.SelectedValue.ToString(),
                            labelar = labelarTextBox.Text,
                            labelfr = labelfrTextBox.Text,
                            abrev = abrevTextBox.Text,
                          //  unite_obligatoire = unite_obligatoireComboBox.SelectedValue.ToString(),
                            type_unite = "UE",
                            coef = coefTextBox.Text,
                            credit = creditTextBox.Text   
                    };

                if (this.iduepereComboBox.SelectedValue !=null &&  this.iduepereComboBox.SelectedValue.ToString() != string.Empty)
                {
                    addue.iduepere = iduepereComboBox.SelectedValue.ToString();
                    addue.type_unite = "ecue";
                }

                if (this.loadingmod == modeload.AJOUT)
                {
                    uvtmoyEntities monentity = new uvtmoyEntities();
                     
                    foreach (scolarite_domaine domaine in listBox2.Items)
                    {

                        addue.scolarite_domaine.Add(monentity.scolarite_domaine.Find(domaine.idScolarite_Domaine));
                    }
                    monentity.scolarite_ensunites.Add(addue);
                   
                     foreach(scolarite_typeeltetude typeelt in listBox4.Items){
                        scolarite_eltetude eltetude = new scolarite_eltetude
                        {
                            idTypeEltEtude = typeelt.idTypeEltEtude,
                            idunite = addue.idunite
                        };
                        monentity.scolarite_eltetude.Add(eltetude);

                    } 
                    monentity.SaveChanges();
                    MessageBox.Show("Nouveau enregistrement a été inseret");
                }
                else
                {

                      uvtmoyEntities monentity = new uvtmoyEntities();
                      addue.scolarite_domaine.Clear();
                      foreach (scolarite_domaine domaine in listBox2.Items)
                      {
                          addue.scolarite_domaine.Add(new scolarite_domaine
                          {
                              idScolarite_Domaine = domaine.idScolarite_Domaine
                          });
                      }

                      monentity.scolarite_ensunites.Attach(addue);
                      monentity.Entry(addue).State =  EntityState.Modified;

                      monentity.SaveChanges();  
                     
                   // GlobalParams.MonDbcontext.SaveChanges();
                    MessageBox.Show("enregistrement a été Modifier");
                }


                this.Close();
                  }
            catch (DBConcurrencyException mes)
            {
                MessageBox.Show(mes.Message);
            }

        }

        private void nature_uniteComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void iduepereComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void labelarLabel_Click(object sender, EventArgs e)
        {

        }

        private void labelarTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btselecteltetude_Click(object sender, EventArgs e)
        {
            List<scolarite_typeeltetude> grp = new List<scolarite_typeeltetude>();


            for (int x = listBox3.Items.Count - 1; x >= 0; x--)
            {
                if (listBox3.GetSelected(x))
                {

                    listBox4.Items.Add(listBox3.Items[x]);

                    // listBox1.Items.RemoveAt(idx);
                }
                else
                {
                    grp.Add((scolarite_typeeltetude)listBox3.Items[x]);
                }
            }


            listBox4.DisplayMember = "labelfr";
            listBox4.ValueMember = "idTypeEltEtude";


            listBox3.DataSource = null;
            listBox3.Items.Clear();
            foreach (var elt in grp)
            {
                listBox3.Items.Add(elt);
            }

            listBox3.DisplayMember = "labelfr";
            listBox3.ValueMember = "idTypeEltEtude";
        }

        private void btdeselecteltetude_Click(object sender, EventArgs e)
        {
            List<scolarite_typeeltetude> grp = new List<scolarite_typeeltetude>();


            for (int x = listBox4.Items.Count - 1; x >= 0; x--)
            {
                if (listBox4.GetSelected(x))
                {
                    listBox3.Items.Add(listBox4.Items[x]);
                }
                else
                {
                    grp.Add((scolarite_typeeltetude)listBox4.Items[x]);
                }
            }


            listBox3.DisplayMember = "labelfr";
            listBox3.ValueMember = "idTypeEltEtude";


            listBox4.Items.Clear();
            foreach (var elt in grp)
            {
                listBox4.Items.Add(elt);
            }

            listBox4.DisplayMember = "labelfr";
            listBox4.ValueMember = "idTypeEltEtude";
        }

        private void btselectdomaine_Click(object sender, EventArgs e)
        {
            List<scolarite_domaine> grp = new List<scolarite_domaine>();


            for (int x = listBox1.Items.Count - 1; x >= 0; x--)
            {
                if (listBox1.GetSelected(x))
                {

                    listBox2.Items.Add(listBox1.Items[x]);

                    // listBox1.Items.RemoveAt(idx);
                }
                else
                {
                    grp.Add((scolarite_domaine)listBox1.Items[x]);
                }
            }


            listBox2.DisplayMember = "labelfr";
            listBox2.ValueMember = "idScolarite_Domaine";


            listBox1.DataSource = null;
            listBox1.Items.Clear();
            foreach (var elt in grp)
            {
                listBox1.Items.Add(elt);
            }

            listBox1.DisplayMember = "labelfr";
            listBox1.ValueMember = "idScolarite_Domaine";
        }

        private void btdeselectdomaine_Click(object sender, EventArgs e)
        {
            List<scolarite_domaine> grp = new List<scolarite_domaine>();


            for (int x = listBox2.Items.Count - 1; x >= 0; x--)
            {
                if (listBox2.GetSelected(x))
                {
                    listBox1.Items.Add(listBox2.Items[x]);
                }
                else
                {
                    grp.Add((scolarite_domaine)listBox2.Items[x]);
                }
            }


            listBox1.DisplayMember = "labelfr";
            listBox1.ValueMember = "idScolarite_Domaine";

            listBox2.Items.Clear();
            foreach (var elt in grp)
            {
                listBox2.Items.Add(elt);
            }

            listBox2.DisplayMember = "labelfr";
            listBox2.ValueMember = "idScolarite_Domaine";
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
